﻿namespace Wp.BLL
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Web;
    using Tools;
    using Wp.DAL;

    public class TblPostsManager : TblPostsManagerBase
    {
        public static TblPostsManager CreateInstant()
        {
            return new TblPostsManager();
        }

        public TblPostsEntity GetTblPostsEntity(DataRow dr, bool getChild)
        {
            TblPostsEntity entity = new TblPostsEntity();
            if (dr.Table.Columns.Contains("Id"))
            {
                entity.Id = dr["Id"].ToInt();
            }
            if (dr.Table.Columns.Contains("TextId"))
            {
                entity.TextId = dr["TextId"].ToString();
            }
            if (dr.Table.Columns.Contains("Title"))
            {
                entity.Title = dr["Title"].ToString();
            }
            if (dr.Table.Columns.Contains("PostType"))
            {
                entity.PostType = dr["PostType"].ToString();
            }
            if (dr.Table.Columns.Contains("Summary"))
            {
                entity.Summary = dr["Summary"].ToString();
            }
            if (dr.Table.Columns.Contains("Description"))
            {
                entity.Description = dr["Description"].ToString();
            }
            if (dr.Table.Columns.Contains("ImageUrl"))
            {
                entity.ImageUrl = dr["ImageUrl"].ToString();
            }
            if (dr.Table.Columns.Contains("Author"))
            {
                entity.Author = dr["Author"].ToString();
            }
            if (dr.Table.Columns.Contains("Url"))
            {
                entity.Url = dr["Url"].ToString();
            }
            if (dr.Table.Columns.Contains("CreatedBy"))
            {
                entity.CreatedBy = dr["CreatedBy"].ToString();
            }
            if (dr.Table.Columns.Contains("CreatedDate"))
            {
                entity.CreatedDate = dr["CreatedDate"].ToDateTime();
            }
            if (dr.Table.Columns.Contains("ModifiedBy"))
            {
                entity.ModifiedBy = dr["ModifiedBy"].ToString();
            }
            if (dr.Table.Columns.Contains("ModifiedDate"))
            {
                entity.ModifiedDate = dr["ModifiedDate"].ToDateTime();
            }
            if (dr.Table.Columns.Contains("DisplayOrder"))
            {
                entity.DisplayOrder = dr["DisplayOrder"].ToInt();
            }
            if (dr.Table.Columns.Contains("ParentId"))
            {
                entity.ParentId = dr["ParentId"].ToInt();
            }
            if (dr.Table.Columns.Contains("PostStatus"))
            {
                entity.PostStatus = dr["PostStatus"].ToString();
            }
            if (dr.Table.Columns.Contains("CommentStatus"))
            {
                entity.CommentStatus = dr["CommentStatus"].ToString();
            }
            if (dr.Table.Columns.Contains("PublishDate"))
            {
                entity.PublishDate = dr["PublishDate"].ToDateTime();
            }
            if (dr.Table.Columns.Contains("TermTaxonomyId"))
            {
                entity.TermTaxonomyId = dr["TermTaxonomyId"].ToInt();
            }
            if (dr.Table.Columns.Contains("TermTaxonomyName"))
            {
                entity.TermTaxonomyName = dr["TermTaxonomyName"].ToString();
            }
            if (dr.Table.Columns.Contains("Slug"))
            {
                entity.Slug = dr["Slug"].ToString();
            }
            if (getChild)
            {
                List<TblPostMetaEntity> list = TblPostMetaManager.CreateInstant().SelectByPostId(entity.Id);
                if (list != null)
                {
                    entity.PostMetas = new Hashtable();
                    foreach (TblPostMetaEntity entity2 in list)
                    {
                        entity.PostMetas.Add(entity2.MetaKey, entity2.MetaValue);
                    }
                }
                entity.lsTermRelationships = TblTermRelationshipsManager.CreateInstant().SelectByObjectId(entity.Id, "category");
            }
            return entity;
        }

        public string GetUniqueTextIdFromUnicodeText(string UnicodeText)
        {
            string sQL = "  SELECT TextId FROM TblPosts tn\r\n                                WHERE tn.TextId=@TextId And PostType='post'";
            return WebUtility.GetUniqueTextIdFromUnicodeText(UnicodeText, "@TextId", sQL, SqlHelper.WpConnectString, 0xff);
        }

        public int InsertPost(string name, string url, int parentid, string des, string posttype)
        {
            try
            {
                TblPostsEntity item = new TblPostsEntity {
                    Id = 0,
                    TextId = UnicodeConversion.CreateInstant().GetStringId(name),
                    Title = name,
                    PostType = posttype,
                    Summary = name,
                    Description = des,
                    ImageUrl = "",
                    Url = url,
                    CreatedBy = HttpContext.Current.User.Identity.Name,
                    Author = HttpContext.Current.User.Identity.Name,
                    CreatedDate = DateTime.Now,
                    ModifiedBy = HttpContext.Current.User.Identity.Name,
                    ModifiedDate = DateTime.Now,
                    DisplayOrder = 1,
                    ParentId = parentid,
                    PostStatus = "publish",
                    CommentStatus = "allow",
                    PublishDate = new DateTime(0x76c, 1, 1)
                };
                return CreateInstant().InsertUpdate(item);
            }
            catch
            {
                return -1;
            }
        }

        public TblPostsEntity SelectOne(int Id, bool allchild)
        {
            SqlParameter[] commandParameters = new List<SqlParameter> { new SqlParameter("@Id", Id) }.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPost", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                return this.GetTblPostsEntity(table.Rows[0], allchild);
            }
            return null;
        }

        public TblPostsEntity SelectOneByTextId(string TextId,string PostType, bool allchild)
        {
            SqlParameter[] commandParameters = new List<SqlParameter> { new SqlParameter("@TextId", TextId), new SqlParameter("@PostType", PostType) }.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPost", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                return this.GetTblPostsEntity(table.Rows[0], allchild);
            }
            return null;
        }

        public List<TblPostsEntity> SelectPopularTblPost(int Top)
        {
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            SqlParameter[] commandParameters = new List<SqlParameter> { new SqlParameter("@Top", Top) }.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectPopularTblPost", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public List<TblPostsEntity> SelectTblPostBy(string PostType, string Title, int TermTaxonomyId, string TermTaxonomyTextId, string Taxonomy, string OrderByEx, string Collate, int PageSize, int PageIndex, out int TotalRow)
        {
            TotalRow = 0;
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            List<SqlParameter> list2 = new List<SqlParameter>();
            SqlParameter item = new SqlParameter("@TotalRow", (int) TotalRow) {
                Direction = ParameterDirection.Output
            };
            list2.Add(item);
            list2.Add(new SqlParameter("@PostType", PostType));
            if (TermTaxonomyId != -1)
            {
                list2.Add(new SqlParameter("@TermTaxonomyId", TermTaxonomyId));
            }
            list2.Add(new SqlParameter("@Title", Title));
            list2.Add(new SqlParameter("@TermTaxonomyTextId", TermTaxonomyTextId));
            list2.Add(new SqlParameter("@Taxonomy", Taxonomy));
            list2.Add(new SqlParameter("@OrderByEx", OrderByEx));
            list2.Add(new SqlParameter("@Collate", Collate));
            list2.Add(new SqlParameter("@PageSize", PageSize));
            list2.Add(new SqlParameter("@PageIndex", PageIndex));
            SqlParameter[] commandParameters = list2.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostBy", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TotalRow = table.Rows[0]["TotalRow"].ToInt();
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public List<TblPostsEntity> SelectTblPostBy(string PostType, string Title, int TermTaxonomyId, string TermTaxonomyTextId, string Taxonomy, string Author, string OrderByEx, string Collate, int PageSize, int PageIndex, out int TotalRow)
        {
            TotalRow = 0;
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            List<SqlParameter> list2 = new List<SqlParameter>();
            SqlParameter item = new SqlParameter("@TotalRow", (int) TotalRow) {
                Direction = ParameterDirection.Output
            };
            list2.Add(item);
            list2.Add(new SqlParameter("@PostType", PostType));
            if (TermTaxonomyId != -1)
            {
                list2.Add(new SqlParameter("@TermTaxonomyId", TermTaxonomyId));
            }
            list2.Add(new SqlParameter("@Title", Title));
            list2.Add(new SqlParameter("@TermTaxonomyTextId", TermTaxonomyTextId));
            list2.Add(new SqlParameter("@Taxonomy", Taxonomy));
            list2.Add(new SqlParameter("@Author", Author));
            list2.Add(new SqlParameter("@OrderByEx", OrderByEx));
            list2.Add(new SqlParameter("@Collate", Collate));
            list2.Add(new SqlParameter("@PageSize", PageSize));
            list2.Add(new SqlParameter("@PageIndex", PageIndex));
            SqlParameter[] commandParameters = list2.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostBy", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TotalRow = table.Rows[0]["TotalRow"].ToInt();
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public List<TblPostsEntity> SelectTblPostBy(string PostType, string Title, int TermTaxonomyId, string TermTaxonomyTextId, string Taxonomy, string Author, DateTime? CreatedDate, string OrderByEx, string Collate, int PageSize, int PageIndex, out int TotalRow)
        {
            TotalRow = 0;
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            List<SqlParameter> list2 = new List<SqlParameter>();
            SqlParameter item = new SqlParameter("@TotalRow", (int) TotalRow) {
                Direction = ParameterDirection.Output
            };
            list2.Add(item);
            list2.Add(new SqlParameter("@PostType", PostType));
            if (TermTaxonomyId != -1)
            {
                list2.Add(new SqlParameter("@TermTaxonomyId", TermTaxonomyId));
            }
            if (CreatedDate.HasValue)
            {
                list2.Add(new SqlParameter("@CreatedDate", CreatedDate.Value));
            }
            list2.Add(new SqlParameter("@Title", Title));
            list2.Add(new SqlParameter("@TermTaxonomyTextId", TermTaxonomyTextId));
            list2.Add(new SqlParameter("@Taxonomy", Taxonomy));
            list2.Add(new SqlParameter("@Author", Author));
            list2.Add(new SqlParameter("@OrderByEx", OrderByEx));
            list2.Add(new SqlParameter("@Collate", Collate));
            list2.Add(new SqlParameter("@PageSize", PageSize));
            list2.Add(new SqlParameter("@PageIndex", PageIndex));
            SqlParameter[] commandParameters = list2.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostBy", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TotalRow = table.Rows[0]["TotalRow"].ToInt();
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public List<TblPostsEntity> SelectTblPostByForAdmin(string PostType, string Title, int TermTaxonomyId, string TermTaxonomyTextId, string Taxonomy, string OrderByEx, string Collate, int PageSize, int PageIndex, out int TotalRow)
        {
            TotalRow = 0;
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            List<SqlParameter> list2 = new List<SqlParameter>();
            SqlParameter item = new SqlParameter("@TotalRow", (int) TotalRow) {
                Direction = ParameterDirection.Output
            };
            list2.Add(item);
            list2.Add(new SqlParameter("@PostType", PostType));
            if (TermTaxonomyId != -1)
            {
                list2.Add(new SqlParameter("@TermTaxonomyId", TermTaxonomyId));
            }
            list2.Add(new SqlParameter("@Title", Title));
            list2.Add(new SqlParameter("@TermTaxonomyTextId", TermTaxonomyTextId));
            list2.Add(new SqlParameter("@Taxonomy", Taxonomy));
            list2.Add(new SqlParameter("@OrderByEx", OrderByEx));
            list2.Add(new SqlParameter("@Collate", Collate));
            list2.Add(new SqlParameter("@PageSize", PageSize));
            list2.Add(new SqlParameter("@PageIndex", PageIndex));
            SqlParameter[] commandParameters = list2.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostByForAdmin", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TotalRow = table.Rows[0]["TotalRow"].ToInt();
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public List<TblPostsEntity> SelectTblPostByParent(int ParentId, string PostType)
        {
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            SqlParameter[] commandParameters = new List<SqlParameter> { new SqlParameter("@ParentId", ParentId), new SqlParameter("@PostType", PostType) }.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostByParent", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public List<TblPostsEntity> SelectTblPostByPostType(string PostType, string PostStatus)
        {
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            List<SqlParameter> list2 = new List<SqlParameter> {
                new SqlParameter("@PostType", PostType)
            };
            if (PostStatus != "")
            {
                list2.Add(new SqlParameter("@PostStatus", PostStatus));
            }
            SqlParameter[] commandParameters = list2.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostByPostType", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public List<TblPostsEntity> SelectTblPostByTags(string Tags, int Top)
        {
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            List<SqlParameter> list2 = new List<SqlParameter>();
            if (!Tags.Equals(""))
            {
                list2.Add(new SqlParameter("@Tags", Tags));
            }
            list2.Add(new SqlParameter("@Top", Top));
            SqlParameter[] commandParameters = list2.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostByTags", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public List<TblPostsEntity> SelectTblPostByVideoViewMore(int Id, int Top)
        {
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            SqlParameter[] commandParameters = new List<SqlParameter> { new SqlParameter("@Id", Id), new SqlParameter("@Top", Top) }.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostByVideoViewMore", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public List<TblPostsEntity> SelectTblPostGetChildFormat(int? Id, string PostType)
        {
            List<TblPostsEntity> list = new List<TblPostsEntity>();
            List<SqlParameter> list2 = new List<SqlParameter>();
            if (Id.HasValue)
            {
                list2.Add(new SqlParameter("@Id", Id));
            }
            list2.Add(new SqlParameter("@PostType", PostType));
            SqlParameter[] commandParameters = list2.ToArray();
            DataTable table = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostGetChildFormat", commandParameters);
            if ((table != null) && (table.Rows.Count > 0))
            {
                TblPostsManager manager = CreateInstant();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(manager.GetTblPostsEntity(table.Rows[i]));
                }
            }
            return list;
        }

        public void UpdateParentOfChild(int Id, int ParentId)
        {
            SqlParameter[] commandParameters = new List<SqlParameter> { new SqlParameter("@Id", Id), new SqlParameter("@ParentId", ParentId) }.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_UpdateParentOfChildTblPosts", commandParameters);
        }
    }
}

